<?php
class MessageController extends UserController{
	public function init()
	{
		parent::init();
		$this->userleftsign='message';
	}

	public function indexAction()
	{
		$this->pagestr='';
		$this->title='我的消息';
		
		//$messageList = $this->getMessageList($this->userinfo['id']);
		
		$page=I('get.page','int',1);
		$pagesize=10;
		
		$pm = M('pm');
		$pmdata = M('pmdata');
		$user = M('user');
		$uid = $this->userinfo['id'];
		
		
		$list = $pm->where("(user_id_a = {$uid} or user_id_b = {$uid})")->order('id desc')->page($page)->limit($pagesize)->select();
		$list_total = $pm->where("(user_id_a = {$uid} or user_id_b = {$uid})")->order('id desc')->count();
		foreach($list as $k=>$v){
			
			
			$last_from = $v['last_from'];
			$last_to = $last_from == 'b' ? 'a' : 'b';
			//var_dump($last_from);
			$userinfo_a = $user->field('id,username')->find($v['user_id_'.$last_from]);
			$userinfo_b = $user->field('id,username')->find($v['user_id_'.$last_to]);
			//var_dump($userinfo_a);
			//名字
			$userinfo_a['username'] = $this->userinfo['id'] == $userinfo_a['id']?'您':$userinfo_a['username'];
			$userinfo_b['username'] = $this->userinfo['id'] == $userinfo_b['id']?'您':$userinfo_b['username'];
			
			//var_dump($userinfo_a['id']);
			
			$v['userinfo_a'] = $userinfo_a;
			$v['userinfo_b'] = $userinfo_b;
			$v['user_status'] = $this->userinfo['id'] == $v['user_id_a']?$v['user_status_a']:$v['user_status_b'];
			$list[$k] = $v;
		}
		
		
		$this->pagestr=page::show($list_total,$pagesize,$page);
		//var_dump($messageList);
		$this->assign("list",$list);
		$this->display();
	}

	public function detailAction()
	{
		$this->title='查看消息';
		$pm_id = I('get.pm_id','int',null);
		if($pm_id){
			$pm = M('pm');
			$pmdata = M('pmdata');
			$user = M('user');
			
			$pminfo = $pm->find($pm_id);
			$last_from = $pminfo['last_from'];
			$last_to = $last_from == 'b' ? 'a' : 'b';
			
			//发送者的未读设为已读
			$pm->where(array('id'=>$pm_id))->update(array('user_status_'.$last_from=>0));
			
			if($this->userinfo['id'] == $pminfo['user_id_'.$last_to]){
				//如果是接受者，设置为已读
				$pm->where(array('id'=>$pm_id))->update(array('user_status_'.$last_to=>0));
			}
			
			//会话列表
			$list = $pmdata->where(array('pm_id'=>$pm_id))->select();
			// var_dump($pmdata->getLastSql());
			// var_dump($list);
			//会员信息
			$userinfo = array();
			$userinfo_a = $user->getinfo($pminfo['user_id_a']);
			$userinfo_b = $user->getinfo($pminfo['user_id_b']);
			$userinfo[$userinfo_a['id']] = $userinfo_a;
			$userinfo[$userinfo_b['id']] = $userinfo_b;
			
			$data = array();
			foreach($list as $k=>$v){
				$v['userinfo_a'] = $userinfo[$v['send_id']];
				$data[$k] = $v;
			}
			$this->assign("userinfo",$this->userinfo);
			$this->assign("list",$data);
			$this->assign("pm_id",$pm_id);
		}else{
			$this->error('参数错误！');
		}
		
		//var_dump($data);
		
		
		
		$this->display();
	}

	public function delAction()
	{
		$this->title='删除消息';
		$this->display();
	}

	public function sendAction()
	{
		$this->title='发新消息';
		
		if(IS_POST){
			$username = I('post.name','str','');
			$content = I('post.intro','str','');
			$lasttime = time();
			if($userinfo_b = M('user')->getinfoByname($username)){
			
				$userinfo_a = $this->userinfo;
				//pm
				$pm = M('pm');
				$temp1 = array('user_id_a'=>$userinfo_a['id'],'user_status_a'=>0,'user_id_b'=>$userinfo_b['id'],'user_status_b'=>1,'last_content'=>$content,'last_datetime'=>$lasttime,'last_from'=>'a');
				$pm_id = $pm->insert($temp1);
				
				//pmdata
				$pmdata = M('pmdata');
				$temp2 = array('pm_id'=>$pm_id,'send_id'=>$userinfo_a['id'],'content'=>$content,'datetime'=>$lasttime);
				$pmdata_id = $pmdata->insert($temp2);
				
				$this->success('发送成功！', U('user.message.index'));
			}
		}
		$this->display();
	}
	public function replayAction()
	{
		$this->title='回复消息';
		
		$pm_id = I('post.pm_id','int','');
		$content = I('post.intro','str','');
		if(!$pm_id && !$content){
			$this->error('参数错误！');
		}
		$pm = M('pm');
		$pmdata = M('pmdata');
		$user = M('user');
		
		$time = time();
		// var_dump($this->userinfo['id']);
		// exit;
		//插入回复信息
		$pmdata->insert(array('send_id'=>$this->userinfo['id'],'pm_id'=>$pm_id,'content'=>$content,'datetime'=>$time));
		
		//更新pm数据
		$pminfo = $pm->find($pm_id);
		//判断回复用户是a 还是b
		if($this->userinfo['id'] == $pminfo['user_id_a']){
			$last_from = 'a';
			$last_to = 'user_status_b';
		}else{
			$last_from = 'b';
			$last_to = 'user_status_a';
		}
		
		//未读数量+1
		$user_status = $pminfo[$last_to]+1;
		$data = $pm->where(array('id'=>$pm_id))->update(array($last_to=>$user_status,'last_content'=>$content,'last_datetime'=>$time,'last_from'=>$last_from));
		
		if($data){
			$this->success('发送成功！', U('user.message.detail',array('pm_id'=>$pm_id)));
		}else{
			$this->error('发送失败！', U('user.message.detail',array('pm_id'=>$pm_id)));
		}
	}
	
}